package com.playball.lottery.frm.yhzx;

import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.base.BaseActivity;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.util.SysApplication;


/**
 * 我要提款
 * 
 * @author Administrator
 * 
 */
public class WithDrawalActivity extends BaseActivity {
	public String loginName;
	public String loginPwd;
	public String bankname;
	public String bankcard;
	public String balance;
	/** 银行ID */
//	public String BankID;
	String bindId;
	public JSONTokener jsonParser;
	private TextView yhzx_wytk_TextView01;
	private TextView yhzx_wytk_TextView02;
	private TextView yhzx_wytk_TextView03;
	private EditText yhzx_wytk_EditText01;
	private EditText yhzx_wytk_EditText02;
	private Button yhzx_wytk_Button01;
	private Button yhzx_wytk_Button02;
	private ShowTipDialog showdialog = new ShowTipDialog();

	// private ApplicationBall app;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.yhzx_wytk);
		// app = (ApplicationBall) getApplication();
		// 初始化控件
		Viewinit();
		AsyncLoaderInfo task = new AsyncLoaderInfo();
		// 获取用户基本信息
		task.execute("107");// 108
		
		SysApplication.getInstance().addActivity(this);
		
		

		// 提交
		yhzx_wytk_Button01.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String balance0 = balance.replaceAll(",", "").trim();
				// Log.e("withd", "balance0= "+balance0);
				if (yhzx_wytk_EditText01.getText().toString().equals("")) {
					Toast.makeText(WithDrawalActivity.this, R.string.withdrawal_amount_not_be_empty, Toast.LENGTH_LONG)
							.show();
				} else if (Integer.parseInt(yhzx_wytk_EditText01.getText().toString()) < 20) {
					Toast.makeText(WithDrawalActivity.this, R.string.least_twenty_yuan, Toast.LENGTH_LONG).show();
				} else if (yhzx_wytk_EditText02.getText().toString().equals("")) {
					Toast.makeText(WithDrawalActivity.this, R.string.enter_the_password_for_withdrawals,
							Toast.LENGTH_LONG).show();
				} else if (yhzx_wytk_EditText02.getText().toString().indexOf(" ") >= 0) {
					Toast.makeText(WithDrawalActivity.this, R.string.not_contain_spaces, Toast.LENGTH_LONG).show();
				} else if (Math.round(Float.parseFloat(balance0)) < Integer.parseInt(yhzx_wytk_EditText01.getText()
						.toString())) {
					Toast.makeText(WithDrawalActivity.this, R.string.amount_is_less_than, Toast.LENGTH_LONG).show();
				} else {

					if (ApplicationData.getInstance().isIsMobileValied()) {
						// 点击提交
						AsyncLoaderSubmit task = new AsyncLoaderSubmit();
						// 用户用户提款
						task.execute("109");// 114
					} else {
						Toast.makeText(WithDrawalActivity.this, R.string.not_by_phone, Toast.LENGTH_LONG).show();
					}

				}

			}
		});

		yhzx_wytk_Button02.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Intent intent = new Intent(getApplicationContext(),
				// BdbankcardActivity.class);
				// startActivity(intent);
				if ("".equals(bankname) || "".equals(bankcard)) {
					Toast.makeText(WithDrawalActivity.this, R.string.perfecting_the_bank_account, Toast.LENGTH_LONG)
							.show();
				}
			}
		});

	}

	public void onResume() {
		super.onResume();
		
	}

	public void onPause() {
		super.onPause();
		
	}

	public void Viewinit() {
		// 标题
		TitleBar();

		yhzx_wytk_TextView01 = (TextView) findViewById(R.id.yhzx_wytk_TextView01);
		yhzx_wytk_TextView01.setText("-----------------");
		yhzx_wytk_TextView02 = (TextView) findViewById(R.id.yhzx_wytk_TextView02);
		yhzx_wytk_TextView02.setText("-----------------");
		yhzx_wytk_TextView03 = (TextView) findViewById(R.id.yhzx_wytk_TextView03);
		yhzx_wytk_TextView03.setText("-------");

		yhzx_wytk_EditText01 = (EditText) findViewById(R.id.yhzx_wytk_EditText01);
		yhzx_wytk_EditText02 = (EditText) findViewById(R.id.yhzx_wytk_EditText02);

		yhzx_wytk_Button01 = (Button) findViewById(R.id.yhzx_wytk_Button01);
		yhzx_wytk_Button02 = (Button) findViewById(R.id.yhzx_wytk_Button02);

	}

	public class AsyncLoaderInfo extends AsyncTask<String, Integer, String> {// 继承AsyncTask

		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(WithDrawalActivity.this);
		}

		// 2.正在后台运行
		@Override
		protected String doInBackground(String... params) {
			String strResult;
			try {
				// 设置请求所需参数
				JSONObject param = new JSONObject();
				// 设置请求action
				String action = "107";
				// 获取请求返回值
				String UserID = ApplicationData.getInstance().getUserID();
				strResult = GetRequestParams.getRequestUserIDResult(param.toString(), UserID, action,
						getApplicationContext());
				return strResult;
			} catch (Exception ex) {
				// TODO: handle exception
				Log.e("error", ex.toString());
				return null;
			}
		}

		// 3.进度更新
		protected void onProgressUpdate(Integer... progress) {
		}

		// 4.完成后台任务
		protected void onPostExecute(String result) {
			if (result != null) {
				try {
					jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					// 接下来的就是JSON对象的操作了
					if (Integer.parseInt(js.getString("Code")) == 0) {
						String rt = js.getString("Data");
						JSONObject job_reult = new JSONObject(rt);
						bankname = job_reult.getString("BankName");
//						BankID = job_reult.optString("BankID", "")//().toString();// BdBankCardUtil.getBankTypeID(bankname);
						bankcard = job_reult.getString("BankCardNumber");
						balance = job_reult.getString("Balance");
						bindId = job_reult.getString("BindID");
						String nobd = "您还未绑定                                ";
						if (!bankname.equals("")) {
							yhzx_wytk_TextView01.setText(bankname);
							yhzx_wytk_Button01.setVisibility(View.VISIBLE);
							yhzx_wytk_Button02.setVisibility(View.GONE);
							System.out.println("11111");
						} else {
							yhzx_wytk_TextView01.setText(nobd);
							yhzx_wytk_Button01.setVisibility(View.GONE);
							yhzx_wytk_Button02.setVisibility(View.VISIBLE);
							System.out.println("444444");
						}
						if (!bankcard.equals("")) {
							yhzx_wytk_TextView02.setText(bankcard);
							System.out.println("33333333");
						} else {
							yhzx_wytk_TextView02.setText(nobd);
						}
						if (!balance.equals("")) {
							yhzx_wytk_TextView03.setText(balance);
							System.out.println("222222222");
						} else {
							yhzx_wytk_TextView03.setText(nobd);
						}
					} else {
						Toast.makeText(WithDrawalActivity.this, js.getString("Data"), Toast.LENGTH_SHORT).show();
					}
				} catch (Exception ex) {
					// 异常处理代码
					Log.e("error", ex.toString());
				} finally {
					showdialog.closeDialog();

					if ("".equals(bankname) || "".equals(bankcard)) {
						Toast.makeText(WithDrawalActivity.this, R.string.perfecting_the_bank_account, Toast.LENGTH_LONG)
								.show();
					}

				}
			} else {
				Toast.makeText(WithDrawalActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}

		// 5.完成后操作
		protected void onCancelled() {
		}

	}

	public class AsyncLoaderSubmit extends AsyncTask<String, Integer, String> {// 继承AsyncTask

		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(WithDrawalActivity.this);
		}

		// 2.正在后台运行
		@Override
		protected String doInBackground(String... params) {
			String strResult;
			try {
				String distillmoney = yhzx_wytk_EditText01.getText().toString();
				String distillpwd = yhzx_wytk_EditText02.getText().toString();
				// 设置请求所需参数
				JSONObject param = new JSONObject();
				param.put("Money", distillmoney);
				param.put("PayPw", distillpwd);
//				param.put("BankID", BankID);
				param.put("BindID", bindId);
				// 设置请求action
				String action = params[0];
				String UserID = ApplicationData.getInstance().getUserID();
				strResult = GetRequestParams.getRequestUserIDResult(param.toString(), UserID, action,
						getApplicationContext());
				return strResult;
			} catch (Exception ex) {
				// TODO: handle exception
				Log.e("error", ex.toString());
				return null;
			}
		}

		// 3.进度更新
		protected void onProgressUpdate(Integer... progress) {
		}

		// 4.完成后台任务
		protected void onPostExecute(String result) {
			if (result != null) {
				try {
					jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (Integer.parseInt(js.getString("Code")) == 0) {
						Toast.makeText(WithDrawalActivity.this, js.getString("Data"), Toast.LENGTH_SHORT).show();
						finish();
					} else {
						Toast.makeText(WithDrawalActivity.this, js.getString("Data"), Toast.LENGTH_SHORT).show();
					}
				} catch (Exception ex) {
					// 异常处理代码
					Log.e("error", ex.toString());
				} finally {
					showdialog.closeDialog();
				}
			} else {
				Toast.makeText(WithDrawalActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
				finish();
			}
		}

		// 5.完成后操作
		protected void onCancelled() {
		}

	}

	// TitleBar标题栏
	public void TitleBar() {
		// Title信息(返回+Title)
		Button back = (Button) findViewById(R.id.top_title_back);
		back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onKeyDown(KeyEvent.KEYCODE_BACK, null);
			}
		});

		TextView info = (TextView) findViewById(R.id.top_title_info);
		info.setText(R.string.yhzx_zjgl_wytk);

		Button refres = (Button) findViewById(R.id.top_title_refres);
		refres.getBackground().setAlpha(255);
		refres.setVisibility(View.INVISIBLE);
		refres.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Intent intent = new Intent(getApplicationContext(),
				// WithDrawalRecordActivity.class);
				// startActivity(intent);
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.finish();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}

	};

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		SysApplication.getInstance().removeActivity(this);
	};
}